package com.site.controller;

import com.site.common.R;
import com.site.common.annotation.Login;
import com.site.common.annotation.Log;
import com.site.pojo.TestPojo;
import com.site.util.ExcelUtil;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * excel 测试
 */
@Controller
@RequestMapping("/excel")
public class ExcelTestController extends BaseController{

    /**
     * 导入excel测试
     * @return
     */
    @Log(module = "Excel",action = "导入Excel")
    @Login(isLogin = false)
    @PostMapping("/import")
    @ResponseBody
    public R<List<TestPojo>> excelImport(MultipartFile file){
        try{
            //excel文件名
            ExcelUtil<TestPojo> excelUtil = new ExcelUtil<>(TestPojo.class);
            //List<TestPojo> pojoList = excelUtil.read("d:/test.xls");
            InputStream inputStream = file.getInputStream();
            List<TestPojo> pojoList = excelUtil.read(inputStream);
            return R.ok(pojoList);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出excel测试
     * @return
     */
    @Log(module = "Excel",action = "导出Excel")
    @GetMapping("/export")
    public R<String> export(HttpServletResponse response) throws Exception {
        //获取数据
        List<TestPojo> list = new ArrayList<>();
        Date date = new Date();
        for (int i = 0; i < 10; i++) {
            TestPojo testPojo = new TestPojo();
            // 空对象导出时为导入模板（只有表头）
            /*testPojo.setId(i);
            testPojo.setName("小池" + i);
            testPojo.setAge(RandomUtils.nextInt(0,2));
            testPojo.setCreateTime(date);
            list.add(testPojo);*/
        }
        //excel文件名
        ExcelUtil<TestPojo> excelUtil = new ExcelUtil<>(TestPojo.class);
        excelUtil.export(response,"用户信息表",list);
        return R.ok();
    }

    // ------------------------------------ 不使用Excel注解 -----------------------------------------------
    /**
     * 导入excel测试
     * @return
     */
    @Log(module = "Excel",action = "导入Excel")
    @Login(isLogin = false)
    @PostMapping("/import2")
    @ResponseBody
    public R<List<Map<String, Object>>> excelImport2(MultipartFile file){
        try{
            // 与 String[] headers = {"用户ID", "用户名称", "用户密码", "用户手机","创建时间"}; 一一对应
            String[] fields = {"id","name","password","time"};
            InputStream inputStream = file.getInputStream();
            List<Map<String, Object>> mapList = ExcelUtil.read2(inputStream, Arrays.asList(fields));
            return R.ok(mapList);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

    /**
     * 导出 excel
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/export2")
    @ResponseBody
    public void export2(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<TestPojo> list = new ArrayList<>();
        Date date = new Date();
        for (int i = 0; i < 10; i++) {
            TestPojo testPojo = new TestPojo();
            testPojo.setId(i);
            testPojo.setName("小池" + i);
            testPojo.setAge(RandomUtils.nextInt(0,2));
            testPojo.setCreateTime(date);
            list.add(testPojo);
        }

        //excel标题
        String[] headers = {"用户ID", "用户名称", "用户密码", "创建时间"};

        //excel sheet名
        String fileName = "用户信息表";

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String [][] content = new String[list.size()][5];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[headers.length];
            TestPojo obj = list.get(i);
            content[i][0] = obj.getId().toString();
            content[i][1] = obj.getName();
            content[i][2] = obj.getAge().toString();
            content[i][3] = dateFormat.format(obj.getCreateTime());
        }
        ExcelUtil.export2(response,fileName,headers,content);
    }
}
